$(function(){
	$("[data-toggle='tooltip']").tooltip();
	$("#from").select2({
        placeholder:"请输入您的出发地",
		minimumInputLength: 1,
	    allowClear: true,
        ajax: {
            type:"POST",
            url: "../php/getCitys.php",
            dataType: 'json',
            data: function (params) {
                return {
                	q:params.term,
                	page: params.page
                };
            },
            processResults: function(data, params){
                var item=[];
                $.each(data.results,function(k,v){
                    item.push({id: v.start_city, text: v.start_city});
                });
            	return{
            		results:item
            	};
            }
        }
	});
	$("#to").select2({
		placeholder: "请输入您的目的地",
		minimumInputLength: 1,
	    allowClear: true,
	    ajax: {
            type:"POST",
            url: "../php/getCitys.php",
            dataType: 'json',
            data: function (params) {
                return {
                	q:params.term,
                	page: params.page
                };
            },
            processResults: function(data, params){
                var item=[];
                $.each(data.results,function(k,v){
                    item.push({id: v.start_city, text: v.start_city});
                });
            	return{
            		results:item
            	};
            }
        }
	});

	$("#getCode").click(function(){
        var phone = $("#phone").val();
        if(phone.length<11){
            $.jGrowl("请输入11位手机号", { life: 3000 });
        }else{
            $.ajax({
                type:"post",
                url:"../php/sendMessage.php",
                data:{"phone":$("#phone").val()},
                success: function(data){
                    if(data == '1'){
                    	time($(".btn-primary").find("#getCode"));
                    	$.jGrowl("短信发送成功", { life: 3000 });
                    }else if(data == '0'){
                        $.jGrowl("短信发送失败", { life: 3000 });
                    }else if(data == '2'){
                        $.jGrowl("您30天内已查询3次", { life: 3000 });
                    }
               }
            }); 
        }
	});

    $("#query").click(function(){
        var from = $("#from");
        var to = $("#to");
        var verify = $("#verify");
        $("#getCode").removeAttr("disabled");
        //js页面验证
        if(from.val()=="" || from.val() == null){
            $.jGrowl("请选择出发地", { life: 3000 });
        }else if(to.val()=="" || to.val()== null){
            $.jGrowl("请选择目的地", { life: 3000 });
        }else if(verify.val() == ""){
            $.jGrowl("请输入验证码", { life: 3000 });
        }else if(verify.val().length >0 && verify.val().length< 4){
            $.jGrowl("请输入4位验证码", { life: 3000 });
        }else{
        	$.ajax({
                type:"post",
                url:"../php/insertQueryInfo.php",
                data:$("#myForm").serializeArray(),
                success: function(data){
                     if(data == '1')
                        $("#myForm").submit();
                     else
                        $.jGrowl("验证码不正确", { life: 3000 });
               }
            });
        }
    });
});
var wait = 60;
function time(btn){
    if (wait===0) {
        $("#getCode").removeAttr("disabled");
        $("#getCode").val("获取验证码");
        wait = 60;
    }else{
        $("#getCode").attr("disabled",true);
        $("#getCode").val(wait + "秒后重试");
        wait--;
        setTimeout(function(){
            time(btn);
        },1000);
    }
}

